{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b4214bdf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "73988190",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "bv 100 depth: 49\n",
      "time_swap:  0.013237476348876953\n",
      "time_greedy:  0.012894630432128906\n",
      "time_optimal:  0.2947506904602051\n",
      "qaoa 100 depth: 498\n",
      "time_swap:  11.750573635101318\n",
      "time_greedy:  11.663041114807129\n",
      "time_optimal:  29.910369237523177\n",
      "qft 100 depth: 690\n",
      "time_swap:  28.85214591026306\n",
      "time_greedy:  31.84979510307312\n",
      "time_optimal:  84.70282578878239\n",
      "vqa 100 depth: 197\n",
      "time_swap:  10.838719606399536\n",
      "time_greedy:  11.005696535110474\n",
      "time_optimal:  30.9478698837135\n"
     ]
    }
   ],
   "source": [
    "# coding=utf-8\n",
    "import qiskit.qasm3\n",
    "from expr_optimal import schedule_optimal\n",
    "from expr_greedy import schedule_greedy\n",
    "from expr_swap import schedule_swap\n",
    "from chiplet import QPU\n",
    "from dag import Circuit_DAG\n",
    "from qiskit import QuantumCircuit\n",
    "import qiskit\n",
    "\n",
    "# 算法类型 bv / qaoa / qft / vqa\n",
    "# 量子比特数 20, 40, 60, 80, 100, 120, 140, 160, 180, 200\n",
    "\n",
    "circuit_type = [\"bv\", \"qaoa\", \"qft\", \"vqa\"]\n",
    "qubit_num = [100]\n",
    "chiplet_type = [\"square\"]\n",
    "\n",
    "chiplet_size = [\n",
    "    {\"square\": 8, \"hexagon\": 5, \"heavy_square\": 5, \"heavy_hexagon\": 3},\n",
    "    {\"square\": 8, \"hexagon\": 5, \"heavy_square\": 5, \"heavy_hexagon\": 3},\n",
    "    {\"square\": 8, \"hexagon\": 5, \"heavy_square\": 5, \"heavy_hexagon\": 3},\n",
    "    {\"square\": 8, \"hexagon\": 5, \"heavy_square\": 5, \"heavy_hexagon\": 3},\n",
    "    {\"square\": 8, \"hexagon\": 5, \"heavy_square\": 5, \"heavy_hexagon\": 3},\n",
    "]\n",
    "\n",
    "chiplet_array = [\n",
    "    {\"square\": [3, 3], \"hexagon\": [3, 3],\n",
    "        \"heavy_square\": [3, 3], \"heavy_hexagon\": [3, 3]},\n",
    "    {\"square\": [3, 3], \"hexagon\": [3, 3],\n",
    "        \"heavy_square\": [3, 3], \"heavy_hexagon\": [3, 3]},\n",
    "    {\"square\": [3, 3], \"hexagon\": [3, 3],\n",
    "        \"heavy_square\": [3, 3], \"heavy_hexagon\": [3, 3]},\n",
    "    {\"square\": [3, 3], \"hexagon\": [3, 3],\n",
    "        \"heavy_square\": [3, 3], \"heavy_hexagon\": [3, 3]},\n",
    "    {\"square\": [3, 3], \"hexagon\": [3, 3],\n",
    "        \"heavy_square\": [3, 3], \"heavy_hexagon\": [3, 3]},\n",
    "]\n",
    "\n",
    "def task(num_idx, num_item, circuit_type_item, chiplet_type_item):\n",
    "    qasm_file = f\"circuits/{circuit_type_item}_{num_item}.qasm\" # QASM文件\n",
    "    ccc = qiskit.qasm3.load(qasm_file)\n",
    "    circuit_depth = ccc.depth()\n",
    "    print(f\"{circuit_type_item} {num_item} depth: {circuit_depth}\")\n",
    "    circuit_dag = Circuit_DAG(qasm_file) # 电路的DAG图\n",
    "    inner_chip_size = chiplet_size[num_idx][chiplet_type_item] # Chip内大小\n",
    "    inter_chip_array = chiplet_array[num_idx][chiplet_type_item] # 芯片矩阵大小\n",
    "    total_count = {\n",
    "        \"swap\": [],\n",
    "        \"greedy\": [],\n",
    "        \"optimal\": [],\n",
    "    }\n",
    "    qpu = QPU(chip_type=chiplet_type_item, inner_chip_size=inner_chip_size, inter_chip_array=inter_chip_array,\n",
    "              circuit_dag=circuit_dag, just_one_map=(circuit_type == \"vqa\" or circuit_type == \"qft\"))\n",
    "    for func_idx, func in enumerate([schedule_swap, schedule_greedy, schedule_optimal]):\n",
    "        max_depth, ancilla_num = func(qpu)\n",
    "        arr = [\"swap\", \"greedy\", \"optimal\"]\n",
    "        total_count[arr[func_idx]].append(max_depth)\n",
    "        total_count[arr[func_idx]].append(ancilla_num)\n",
    "\n",
    "for num_idx, num_item in enumerate(qubit_num):  # 100\n",
    "    for circuit_type_item in circuit_type:  # \"bv\", \"qaoa\", \"qft\", \"vqa\"\n",
    "        for chiplet_type_item in chiplet_type:  # \"square\"\n",
    "            task(num_idx, num_item, circuit_type_item, chiplet_type_item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bf33d117",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.00027015257854850924\n",
      "0.00026315572310467157\n",
      "0.006015320213473573\n",
      "0.0023738532596164277\n",
      "0.0023561699221832585\n",
      "0.006042498835863268\n",
      "0.002914358172753845\n",
      "0.0032171510205124365\n",
      "0.008555840988765898\n",
      "0.002189640324525159\n",
      "0.0022233730373960553\n",
      "0.006252094926002727\n"
     ]
    }
   ],
   "source": [
    "depths = [49, 4950, 9900, 4950]\n",
    "for idx, i in enumerate([[0.013237476348876953,\n",
    "0.012894630432128906,\n",
    "0.2947506904602051,],\n",
    "[11.750573635101318,\n",
    "11.663041114807129,\n",
    "29.910369237523177,],\n",
    "[28.85214591026306,\n",
    "31.84979510307312,\n",
    "84.70282578878239,],\n",
    "[10.838719606399536,\n",
    "11.005696535110474,\n",
    "30.9478698837135,]]):\n",
    "    for j in i:\n",
    "        print(j / depths[idx])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "cplex",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
